<?php
/**
 * 资讯管理
 * @author weihu
 *
 */
class Advisory extends MY_Admin_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Advisory_model');
    }
    
    //资讯列表
    public function index()
    {
        $title = $this->input->get_post('title');
        $begtime = $this->input->get_post('begtime');
        $endtime = $this->input->get_post('endtime');
        $orderby = $this->input->get_post('orderby');
        $tag = $this->input->get_post('tag');
        $type = $this->input->get_post('type');
        
        $page     = _get_page();
        $pagesize = 10;
        $arrParam = array();
        $arrWhere = array();
        
        if($title)
        {
            $arrParam['title'] = $title;
            $arrWhere['title like '] = "'%$title%'";
        }
        
        if ($tag)
        {
            $arrParam['tag'] = $tag;
            $arrWhere['tag like '] = "'%$tag%'";
        }
        if ($type)
        {
            $arrParam['type'] = $type;
            $arrWhere['type'] = $type;
        }
        
        if($begtime)
        {
            $arrParam['begtime'] = $begtime;
            $arrWhere["advisory_date >="] = strtotime($begtime);
        }
        if($endtime)
        {
            $arrParam['endtime'] = $endtime;
            $arrWhere["advisory_date <="] = strtotime("$endtime 23:59:59");
        }
        $strOrder = 'id desc';
        if($orderby)
        {
            $arrParam['orderby'] = $orderby;
            $strOrder = $orderby;
        }
        
        $list = $this->Advisory_model->fetch_page($page, $pagesize, $arrWhere, 'id,title,content,advisory_img,advisory_date,type,tag,op_username,is_recommend', $strOrder);
        //echo $this->db->last_query();die;
        
        //分页
        $pagecfg = array();
        $pagecfg['base_url']     = _create_url('admin/advisory', $arrParam);
        $pagecfg['total_rows']   = $list['count'];
        $pagecfg['cur_page'] = $page;
        $pagecfg['per_page'] = $pagesize;
        //$this->load->library('pagination');
        $this->pagination->initialize($pagecfg);
        $list['pages'] = $this->pagination->create_links();
        
        $result = array(
            'list' => $list,
            'arrParam' => $arrParam,
        );
        
        $this->load->view('admin/advisory',$result);
    }
    
    //添加资讯
    public function add()
    {
        //需要修改
        $id	= _get_key_val($this->input->get('id'), TRUE);
        $result = array();
        $info = array();
        
        if(!empty($id))
        {
            $info = $this->Advisory_model->get_by_id($id);
        }
        
        $result = array(
            'info'=>$info,
        );
        
        $this->load->view('admin/advisory_add', $result);
    }
    
    //删除资讯
    public function del()
    {
        $id	= _get_key_val($this->input->get('id'), TRUE);
        $page = _get_page();
        
        $this->Advisory_model->delete_by_id($id);
        redirect( base_url('/admin/advisory?page='.$page) );
    }
    
    //保存
    public function save()
    {
        if ($this->input->is_post())
        {
            //验证规则
            $config = array(
                array(
                    'field'   => 'title',
                    'label'   => '资讯标题',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'tag',
                    'label'   => '资讯标签',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'type',
                    'label'   => '类型',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'advisory_img',
                    'label'   => '资讯图片',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'content',
                    'label'   => '资讯内容',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'advisory_date',
                    'label'   => '资讯日期',
                    'rules'   => 'trim|required'
                ),
            );
        
            $this->form_validation->set_rules($config);
        
            if ($this->form_validation->run() === TRUE)
            {
                //将需要保存的数据赋值给数组$data
                $data = array(
                    'title'=>$this->input->post('title'),
                    'tag'=>$this->input->post('tag'),
                    'type'=>$this->input->post('type'),
                    'content'=>$this->input->post('content'),
                    'advisory_img'=>$this->input->post('advisory_img'),
                    'add_time'=>time(),
                    'update_time'=>time(),
                    'advisory_desc'=>htmlspecialchars($_POST['editorValue']),
                    'advisory_date'=>strtotime($this->input->post('advisory_date')),
                    'op_userid'=>$this->session->userdata['admin_id'],
                    'op_username'=>$this->session->userdata['user_name'],
                );
        
                $id	= _get_key_val($this->input->get('id'), TRUE);
                if($id)
                    $data['id'] = $id;
                //保存至数据库
                $this->Advisory_model->insert($data);
        
                //echo '成功,<a href="/admin/aa">返回列表页</a>';
                redirect(base_url('/admin/advisory'));
                exit;
            }
            else
            {
                $id	= _get_key_val($this->input->get('id'), TRUE);
                $result = array();
        
                if(!empty($id))
                {
                    $info = $this->Advisory_model->get_by_id($id);
                    $result = array(
                        'info'=>$info,
                    );
                }
                $this->load->view('admin/advisory_add',$result);
            }
        }
    }
    
    //推荐
    function recommend(){
        $id	= _get_key_val($this->input->get('id'), TRUE);
        $page = _get_page();
    
        $is_recommend = 1;
        $info = $this->Advisory_model->get_by_id($id);
        if ($info && $info['is_recommend'] == 1)
        {
            $is_recommend = 0;
        }
        
        //将需要保存的数据赋值给数组$data
        $data = array(
            'id'=>$id,
            'is_recommend'=>$is_recommend,
            'op_userid'=>$this->session->userdata['admin_id'],
            'op_username'=>$this->session->userdata['user_name'],
        );
        
        //保存至数据库
        $this->Advisory_model->insert($data);
    
        redirect( base_url('/admin/advisory?page='.$page) );
    
    }
}